package com.lzh.eduservice.controller;
import com.lzh.commonutils.JwtUtils;
import com.lzh.commonutils.R;
import com.lzh.commonutils.vo.UcenterMemberVo;
import com.lzh.eduservice.client.UcenterClient;
import com.lzh.eduservice.entity.EduComment;
import com.lzh.eduservice.entity.vo.CommentQueryVo;
import com.lzh.eduservice.service.EduCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author lzh
 * @since 2021-03-11
 */
@RestController
@RequestMapping("/eduservice/comment")
@CrossOrigin
public class EduCommentController {

    @Autowired
    private EduCommentService eduCommentService;

    @Autowired
    private UcenterClient ucenterClient;

    /**
     *  分页查询评论
     *  方法名：getCommentPageInfo
     *  参数：String current,String page,查询对象
     *  返回值：R
     */
    @PostMapping("getCommentPageInfo/{current}/{limit}")
    public R getCommentPageInfo(@PathVariable long current, @PathVariable long limit,
                                @RequestBody(required = false) CommentQueryVo commentQueryVo){
        //调用service，返回Map
        Map<String, Object> map = eduCommentService.getCommentPageInfo(current, limit, commentQueryVo);

        //返回
        return R.ok().data(map);
    }

    /**
     *  添加评论
     *  方法名：addComment
     *  参数：EduComment comment,HttpServletRequest request
     *  返回值：R
     */
    @PostMapping("addComment")
    public R addComment(@RequestBody(required = false) EduComment comment, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        UcenterMemberVo memberVo = ucenterClient.getMemberInfo(memberId);

        System.out.println("memberVo:::"+memberVo);

        //设置到comment
        comment.setMemberId(memberVo.getId());
        comment.setNickname(memberVo.getNickname());
        comment.setAvatar(memberVo.getAvatar());

        //添加到数据库中
        boolean flag = eduCommentService.save(comment);

        if(flag){
            //返回
            return R.ok();
        }else {
            return R.error();
        }

    }

}

